package org.example.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.NoteMapper;
import org.example.pojo.Notes;
import org.example.pojo.PageBean;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/note")
public class NoteController {
    @Autowired
    private NoteMapper noteMapper;
    @GetMapping("/{userId}")
    public Result noteList(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @PathVariable("userId") Integer userId, String sorted) {
            PageHelper.startPage(page, size);
            List<Notes> notesList = noteMapper.noteList(userId, sorted);
            Page<Notes> p = (Page<Notes>) notesList;
            PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
            return Result.success(pageBean);
    }
    @PostMapping
    public Result addNote(@RequestBody Notes notes) {
            noteMapper.addNote(notes);
            return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result delNote(@PathVariable("id") Integer id) {
            noteMapper.delNote(id);
            return Result.success();
    }
    @PutMapping
    public Result updateNote(@RequestBody Notes notes) {
            noteMapper.updateNote(notes);
            return Result.success();
    }
}
